// 全局状态管理
const AppState = {
    userInfo: null,
    membershipInfo: null,
    statsData: null,
    
    // 初始化时从localStorage加载
    init() {
        try {
            this.userInfo = JSON.parse(localStorage.getItem('user_info'));
            this.membershipInfo = JSON.parse(localStorage.getItem('membership_info'));
        } catch (error) {
            console.error('初始化状态失败:', error);
            this.clear();
        }
    },
    
    // 更新用户信息
    setUserInfo(info) {
        this.userInfo = info;
        localStorage.setItem('user_info', JSON.stringify(info));
    },
    
    // 更新会员信息
    setMembershipInfo(info) {
        this.membershipInfo = info;
        localStorage.setItem('membership_info', JSON.stringify(info));
    },
    
    // 清除所有状态
    clear() {
        this.userInfo = null;
        this.membershipInfo = null;
        localStorage.removeItem('user_info');
        localStorage.removeItem('membership_info');
    },
    
    setStatsData(data) {
        this.statsData = data;
        // 触发状态更新事件
        document.dispatchEvent(new CustomEvent('statsDataChanged', { detail: data }));
    },
    
    clearStatsData() {
        this.statsData = null;
    }
};

// 初始化状态
AppState.init();

// 监听统计数据变化
document.addEventListener('statsDataChanged', (event) => {
    const stats = event.detail;
    if (stats) {
        // 更新UI显示
        document.getElementById('total_cursors').textContent = stats.total_cursors.toLocaleString();
        document.getElementById('unused_cursors').textContent = stats.unused_cursors.toLocaleString();
        document.getElementById('used_cursors').textContent = stats.used_cursors.toLocaleString();
    }
}); 